package com.eleadmin.store.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.store.entity.Sell;
import com.eleadmin.store.service.SellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 销售单
 * @author : Doman
 * @date : 2024-03-07 14:12:18
 * @version ：1.0
 */

@Api(tags = "销售单")
@RestController
@RequestMapping("/api/store/sell")
public class SellController extends BaseController {

    @Autowired
    private SellService sellService;

    @PreAuthorize("hasAuthority('store:sell:list')")
    //@ApiOperation("分页查询销售单")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Sell sell) throws Exception{
        return sellService.list(page, sell, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:sell:list')")
    //@ApiOperation("查询全部销售单")
    @GetMapping("sellNums")
    public JsonResult sellNums(Sell sell){
        return sellService.sellNums(sell, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:sell:list')")
    //@ApiOperation("查询全部销售单")
    @GetMapping()
    public JsonResult list(Sell sell){
        return sellService.list(sell, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:sell:list')")
    //@ApiOperation("根据id查询销售单")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return sellService.getById(id);
    }


    @PreAuthorize("hasAuthority('store:sell:save')")
    @OperationLog
    @ApiOperation(value ="添加销售单")
    @PostMapping()
    public JsonResult save(@RequestBody Sell sell) {
        try {
            return sellService.save(sell, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('store:sell:update')")
    @OperationLog
    @ApiOperation(value ="修改销售单")
    @PutMapping()
    public JsonResult update(@RequestBody Sell sell) {
        try {
            return sellService.update(sell, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:sell:update')")
    @OperationLog
    @ApiOperation(value ="修改收款,开票状态")
    @PutMapping("updateTableState")
    public JsonResult updateTableState(@RequestBody Sell sell) {
        try {
            return sellService.updateTableState(sell, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:sell:remove')")
    @OperationLog
    @ApiOperation(value ="删除销售单")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return sellService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:sell:remove')")
    @OperationLog
    @ApiOperation(value ="删除销售单")
    @DeleteMapping("removeSellAndSon//{id}")
    public JsonResult removeSellAndSon(@PathVariable("id") String id) {
        try {
            return sellService.removeSellAndSon(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
